package com.hrms.service;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;

import com.google.appengine.api.mail.MailService;
import com.google.appengine.api.mail.MailServiceFactory;
import com.hrms.core.Constants;

/**
 * Servlet implementation class SendMailService
 *
 * @web.servlet
 *   name="SendMailService"
 *   display-name="SendMailService"
 *
 * @web.servlet-mapping
 *   url-pattern="/tasks/SendMail.do"
 */
public class SendMailService extends HttpServlet {
	private static final long serialVersionUID = 1L;
	private static final Logger log = Logger.getLogger(SendMailService.class.getName());
	public static final String SUBJECT = "subject";
	public static final String MSGBODY = "msgBody";
	public static final String TO = "to";
	public static final String TO_SIZE = "toSize";
	private static final MailService mailService = MailServiceFactory.getMailService(); 
	public static final String APP_MAIL = "Team Dashboard <mail@"+ Constants.APP_ID +".appspotmail.com>";
	public static final String COPY_ADMINS = "copyAdmins";
	
    /**
     * @see HttpServlet#HttpServlet()
     */
    public SendMailService() {
        super();
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		log.info("Got work");
		//boolean copyAdmins = request.getParameter(SendMailService.COPY_ADMINS)!=null;
			
			String subject = StringUtils.isNotBlank(request.getParameter(SUBJECT)) ? request.getParameter(SUBJECT)
					: com.hrms.core.Constants.EMPTY_STRING;
			
			String msgBody = StringUtils.isNotBlank(request.getParameter(MSGBODY)) ? request.getParameter(MSGBODY)
					: com.hrms.core.Constants.EMPTY_STRING;
			
			try {
			
			List<String> recipients = new ArrayList<String>();
			//String[] recipients = request.getParameterValues(TO);
			
			int count=0;
			while(true){
				if(request.getParameter(TO.concat(String.valueOf(count))) != null){
					recipients.add( request.getParameter(TO.concat(String.valueOf(count))) );
					log.info(request.getParameter(TO.concat(String.valueOf(count))));
					count++;
				}else
					break;
			}
			
			log.info("Recipients "+recipients.size());
			log.info(subject);
			log.info(msgBody);			
			
			if(count > 0){
				
				MailService.Message message = new MailService.Message();
				message.setSender(APP_MAIL);
				message.setTo(recipients);
				message.setSubject(subject);
				message.setTextBody(msgBody);
					
			/*if(copyAdmins){
				mailService.sendToAdmins(message);	
			}else{*/
				mailService.send(message);
		//	}
			}
				log.info("sent");
			} catch (Exception e) {
				log.log(Level.SEVERE,
						"Failed to Send Email [to: "
								 + ",subject: "
								+ subject + ", msgBody: " + msgBody + "]", e);
				response.sendError(500);
			}
	}



	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doPost(request, response);
	}

}
